package baseClass;

import baseAPI.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Room {
    public final static int SINGLE = 1;
    public final static int DOUBLE = 2;
    public final static int LUXURY = 3;
    public final static int PRESIDENT = 4;
    public final static int FAMILY = 5;
    public final static int COUPLE = 6;

    private final int roomNumber;
    private int roomKind;
    private int price;
    private int status;

    //没有无参构造函数，不允许建立空对象
    public Room(int roomNumber, int roomKind, int price, int status){//新建room对象
        this.roomNumber = roomNumber;
        this.roomKind = roomKind;
        this.price = price;
        this.status = status;
    }

    public String[] getRoomInformation(){//返回房间信息
        return new String[]{String.valueOf(roomNumber), String.valueOf(roomKind), String.valueOf(price), String.valueOf(status)};
    }

    public int getRoomKind(){//获取房间种类
        return roomKind;
    }

    public int getRoomNumber(){
        return roomNumber;
    }

    public int getPrice(){
        return price;
    }

    public static int getPrice(int roomKind, Database database){
        ResultSet resultSet = database.select("price", "room", "roomKind = " + roomKind + " limit 1");
        try{
            if(!resultSet.next()){
                return -1;
            }
            else{
                return resultSet.getInt(1);
            }
        }catch (SQLException ex){
            System.out.println("getPriceException");
        }
        return -1;
    }

    public void setRoomKind(int roomKind, Database database){//修改房间种类
        this.roomKind = roomKind;
        database.update("room", "roomKind", String.valueOf(roomKind), "roomNumber = \"" + roomNumber + "\"");
    }

    public static void setPrice(int roomKind, int price, Database database){//修改房间价格
        database.update("room", "price", String.valueOf(price), "roomKind = " + roomKind);
    }

    public int getStatus(){
        return this.status;
    }

    public static int getStatus(int roomNumber, Database database){
        ResultSet resultSet =  database.select("status", "room", "roomNumber = " + roomNumber);
        try{
            resultSet.next();
            return resultSet.getInt(1);
        }catch (SQLException ex){
            System.out.println("getStatusException");
        }
        return -1;
    }

    public void setStatus(int status, Database database){//修改房间状态
        this.status = status;
        database.update("room", "status", String.valueOf(status), "roomNumber= \"" + roomNumber + "\"");
    }

    public static void setStatus(int status, int roomNumber, Database database){
        database.update("room", "status", String.valueOf(status), "roomNumber = " + roomNumber);
    }

    public void exportToDatabase(Database database){
        database.insert("room", "\"" + roomNumber + "\"" + "," + roomKind + "," + price + "," + status);
    }

    public static Room getThroughRoomNumber(int roomNumber, Database database){
        ResultSet resultSet = database.select("*", "room", "roomNumber = " + roomNumber);
        try {
            if (!resultSet.next()) {
                return null;
            }
            else{
                return new Room(resultSet.getInt(1) ,resultSet.getInt(2), resultSet.getInt(3), resultSet.getInt(4));
            }
        }catch(SQLException ex){
            System.out.print("getFromDatabaseException");
        }
        return null;
    }

    public static ArrayList<Room> getThroughRoomKind(int roomKind, Database database){
        ResultSet resultSet = database.select("*", "room", "roomKind = " + roomKind);
        return getRooms(resultSet);
    }

    public static ArrayList<Room> getThroughRoomStatus(int roomStatus, Database database){
        ResultSet resultSet = database.select("*", "room", "status = " + roomStatus);
        return getRooms(resultSet);
    }

    private static ArrayList<Room> getRooms(ResultSet resultSet) {
        ArrayList<Room> rooms = new ArrayList<>();
        try{
            if(!resultSet.next()){
                return null;
            }
            else{
                do{
                    rooms.add(new Room(resultSet.getInt(1) ,resultSet.getInt(2), resultSet.getInt(3), resultSet.getInt(4)));
                }while(resultSet.next());
                return rooms;
            }
        }catch(SQLException ex){
            System.out.print("getRoomsException");
        }
        return null;
    }
}
